 <?php
/**
 * 地区类
 * @author jf
 *
 */
	class area extends common{
		
		private $province_table = "pcc_province";		//省
		private $city_table = "pcc_city";				//市
		private $county_table = "pcc_county";			//区
		private $circle_table = "pcc_circle";			//商圈
		private $hot_city_table='hot_city';				//热门城市
		
		/**
		 * 获取一个省的信息
		 * @param int $id 省id
		 * @param string  $key
		 * @return array
		 */
		function province_one($param){
			extract($param);
			if($id != null && $id != ""){
				$sql = $this->province_table." WHERE provinceid=".$id;
				$res = $this->getInfo($sql);
			}
			return $res;
		}
		
		/**
		 * 获取一个城市信息
		 * @param int $id
		 * @param string $key
		 * @return array <result, multitype:>
		 */
		function city_one($param){
			extract($param);
			if($id != null && $id != ""){
				$sql = $this->city_table." WHERE cityid='".$id."' or city='".$id."'";
				$res = $this->getInfo($sql);
			}
			return $res;
		}
		
		/**
		 * 获取一个区的信息
		 * @param int $id
		 * @param string $key
		 * @return array
		 */
		function county_one($param){
			extract($param);
			if($id != null && $id != ""){
				$sql = $this->county_table." WHERE countyid=".$id;
				$res = $this->getInfo($sql);
			}
			return $res;
		}
		
		/**
		 * 获取一个商圈信息
		 * @param int $id
		 * @param string $key
		 * @return Ambigous <result, multitype:>
		 */
		function circle_one($param){
			extract($param);
			if($id != null && $id != ""){
				$sql = $this->circle_table." WHERE circleid=".$id;
				$res = $this->getInfo($sql,$key);
			}
			return $res;
		}
		
		/**
		 * 获得所有省
		 * @author	jf
		 * @param 	string 	$key  查询字段
		 * @return 	array
		 */
		function province_list(){
			$sql = $this->province_table." WHERE fatherid=''";
			$res = $this->getList($sql);
			return $res['result'];
		}
		
		/**
		 * 获得某个省内的所有城市
		 * @author	jf
		 * @param 	int 	$fid  省Id
		 * @param 	string 	$key  查询字段
		 * @return 	array
		 */
		function city_list($param){
			extract($param);
			if($fid != null && $fid != ""){
				$sql = $this->city_table." WHERE fatherid='".$fid."'";
				$res = $this->getList($sql);
			}
			return $res['result'];
		}
		
		/**
		 * 获得某个城市内的所有地区
		 * @author	jf
	 	 * @param 	int 	$fid  城市Id
	 	 * @param 	string 	$key  查询字段
		 * @return 	array 
	 	 */
		function county_list($param){
			extract($param);
			if($fid != null && $fid != ""){
				$sql = $this->county_table." WHERE fatherid=".$fid;
				$res = $this->getList($sql);
			}
			return $res['result'];
		}
		
		/**
		 * 获得某个区内的所有商圈
		 * @author	jf
		 * @param 	ind 	$fid  区域id
		 * @param	string  $key  查询字段
		 * @return 	array
		 */
		function circle_list($param){
			extract($param);
			if($fid != null && $fid != ""){
				$sql = $this->circle_table." WHERE fatherid=".$fid;
				$res = $this->getList($sql);
			}
			return $res['result'];
		}
		
		/**
		 * 地区重组（条件搜索使用）
		 * @author	jf
		 * @param	array	$arr		要重组的分类
		 * @param	array	$get		url参数
		 * @param	string	$url_name	url名
		 * @param	string	$key		地区-p(省),c(市),co(区),ci(商圈)
		 * @return	array
		 */
		function county_list_re($arr,$get,$url_name,$key='co'){
			if(!empty($arr)){
				//添加不限选项
				$url=$get;
				unset($url['co']);
				unset($url['ci']);
				@$urls = http_build_query($url);
				if($get[$key] != null && $get[$key] != ""){
					$is_check = 0;
				}else{
					$is_check = 1;
				}
				if(!empty($url)){
					$url_re=$url_name."?".$urls;
				}else{
					$url_re=$url_name;
				}
				$row[0]= array('county'=>"不限",'url'=>$url_re,'is_check'=>$is_check);
				//重组其他选项值
				for($i=0;$i<count($arr);$i++){
					$url = $get;
					unset($url['ci']);
					$url[$key] = $arr[$i]['countyid'];
					if($get[$key] == $arr[$i]['countyid']){
						$arr[$i]['is_check'] = 1;			//该元素被选中
					}else{
						$arr[$i]['is_check'] = 0;
					}
					$urls = http_build_query($url);			//将数组构造成url参数
					$arr[$i]['url'] = $url_name."?".$urls;
					$row[$i+1] = $arr[$i];
				}
			}
			return $row;
		}
		
		
		/**
		 * 地区重组（条件搜索使用）
		 * @author	jf
		 * @param	array	$arr		要重组的分类
		 * @param	array	$get		url参数
		 * @param	string	$url_name	url名
		 * @param	string	$key		地区-p(省),c(市),co(区),ci(商圈)
		 * @return	array
		 */
		function circle_list_re($arr,$get,$url_name,$key='ci'){
			//添加不限选项
			$url=$get;
			unset($url['ci']);
			$urls = http_build_query($url);
			if($get[$key] != null && $get[$key] != ""){
				$is_check = 0;
			}else{
				$is_check = 1;
			}
			if(!empty($url)){
				$url_re=$url_name."?".$urls;
			}else{
				$url_re=$url_name;
			}
			$row['list'][0]= array('circle'=>"不限商圈",'url'=>$url_re);
			if(!empty($arr)){
				//重组其他选项值
				for($i=0;$i<count($arr);$i++){
					$url = $get;
					$url[$key] = $arr[$i]['circleid'];
					if($get[$key] == $arr[$i]['circleid']){
						$row['xz'] = $arr[$i]['circle'];		//该元素被选中
					}
					$urls = http_build_query($url);			//将数组构造成url参数
					$arr[$i]['url'] = $url_name."?".$urls;
					$row['list'][$i+1] = $arr[$i];
				}
			}
			if($row['xz'] == ""){
				$row['xz'] = "不限商圈";			//选中的值
			}
			return $row;
		}

		/**
		 * 热门城市
		 * @param  string  $orderby [description]
		 * @param  string  $key     [description]
		 * @param  integer $num     [description]
		 * @return array           [description]
		 * @author wxy <[email address]>
		 */
		function hot_city($param){
			extract($param);
			$sql=$this->hot_city_table." h order by h.sort desc limit 0,".$num;
			$hots=$this->getList($sql);
			return $hots['result'];
		}
		
		function citys(){
			// 查询所有城市
			$city = $this->getList('pcc_city');
			
			$res = array('A'=>'','B'=>'','C'=>'','D'=>'','E'=>'','F'=>'','G'=>'','H'=>'','I'=>'','J'=>'','K'=>'','L'=>'','M'=>'','N'=>'','O'=>'','P'=>'','Q'=>'','R'=>'','S'=>'','T'=>'','U'=>'','V'=>'','W'=>'','X'=>'','Y'=>'','Z'=>'');
			
			foreach($res as $k=>$v){
				for ($i=0; $i < count($city['result']); $i++){
					$city['result'][$i]['type'] = $this->getfirstchar($city['result'][$i]['city']);
					if($k==$city['result'][$i]['type']){
						$res[$k][] = $city['result'][$i];
					}
				}
			}
			
			return $res;
		}
		
		function getfirstchar($s0){
			$firstchar_ord = ord(strtoupper($s0{0}));
			if(($firstchar_ord >= 65 and $firstchar_ord <= 91) or ($firstchar_ord >= 48 and $firstchar_ord <= 57))
				return $s0{0};
			$s = iconv("UTF-8", "gb2312", $s0);
			$asc = ord($s{0}) * 256 + ord($s{1}) - 65536;
			if($asc >= - 20319 and $asc <= - 20284)
				return "A";
			if($asc >= - 20283 and $asc <= - 19776)
				return "B";
			if($asc >= - 19775 and $asc <= - 19219)
				return "C";
			if($asc >= - 19218 and $asc <= - 18711)
				return "D";
			if($asc >= - 18710 and $asc <= - 18527)
				return "E";
			if($asc >= - 18526 and $asc <= - 18240)
				return "F";
			if($asc >= - 18239 and $asc <= - 17923)
				return "G";
			if($asc >= - 17922 and $asc <= - 17418)
				return "H";
			if($asc >= - 17417 and $asc <= - 16475)
				return "J";
			if($asc >= - 16474 and $asc <= - 16213)
				return "K";
			if($asc >= - 16212 and $asc <= - 15641)
				return "L";
			if($asc >= - 15640 and $asc <= - 15166)
				return "M";
			if($asc >= - 15165 and $asc <= - 14923)
				return "N";
			if($asc >= - 14922 and $asc <= - 14915)
				return "O";
			if($asc >= - 14914 and $asc <= - 14631)
				return "P";
			if($asc >= - 14630 and $asc <= - 14150)
				return "Q";
			if($asc >= - 14149 and $asc <= - 14091)
				return "R";
			if($asc >= - 14090 and $asc <= - 13319)
				return "S";
			if($asc >= - 13318 and $asc <= - 12839)
				return "T";
			if($asc >= - 12838 and $asc <= - 12557)
				return "W";
			if($asc >= - 12556 and $asc <= - 11848)
				return "X";
			if($asc >= - 11847 and $asc <= - 11056)
				return "Y";
			if($asc >= - 11055 and $asc <= - 10247)
				return "Z";
			return null;
		}
		
	}